<?php
/**
 * 前台公用behavior
 * Enter description here ...
 * @author nijn
 *
 */
class FrontBehavior extends CActiveRecordBehavior{

	/**
	 * 字段
	 * Enter description here ...
	 * @var unknown_type
	 */
	public $attributes = array(
		'title' => 'title',
		'img' => 'thumb',
		'time' => 'create_time',
	    'desc' => 'desc',
	);
	public $linkProvince = array(
		'province', 'city', 'district'
	);
	
	/**
	 * 路径（Module/Controller）
	 * Enter description here ...
	 * @var string
	 */
	public $route = '';
	public $list = 'list';
	public $view = 'view';
	
	public function getCTime() {
		$this->issetAttribute('time');
		
		if (is_array($this->attributes['time'])){
			$format = isset($this->attributes['time']['format']) ? $this->attributes['time']['format'] : 'Y-m-d H:i';
			$time = date($format, strtotime($this->owner->{$this->attributes['time']['attribute']}));
		} else 
			$time = $this->owner->{$this->attributes['time']};
			
		return $time;
		
	}
	
	public function getDetailProvince($length = 0) {
		$str = '';
		foreach ($this->linkProvince as $one) {
			$str .= ' '.Yii::app()->enum->lookup('district', CHtml::value($this->owner, $one));
		}
		if ($length > 0)
			$str = CUtilHelper::string()->msubstr($str,0, $length, 'utf-8', '..');
		return $str;
	}
	
	/**
	 * 获取图片
	 * Enter description here ...
	 * @param int $width
	 * @param int $height
	 * @param array $htmlOptions
	 */
	public function img($width = 200, $height = 100, $htmlOptions = array(), $cut = 0) {
		$this->issetAttribute('img');
		return Yii::app()->img->config('default')->out(
			$this->owner->{$this->attributes['img']},
			array_merge($htmlOptions,array(
				'width' => $width,
				'height' => $height,
			)), array('width' => $width, 'height' => $height, $cut)
		);
	}
	
	/**
	 * 获取图片路径
	 * Enter description here ...
	 */
	public function getVImg($width = null, $height = null, $cut = false) {
		$this->issetAttribute('img');
		return Yii::app()->img->config('default')->url(
			$this->owner->{$this->attributes['img']},
			$width, $height, $cut
		);
	}
	/**
	 * 获取标题
	 * Enter description here ...
	 * @param unknown_type $length
	 */
	public function title($length = 0, $encode = false) {
		$this->issetAttribute('title');
		$title = $this->owner->{$this->attributes['title']};
		if (is_numeric($length) && $length > 0) {
			$title = CUtilHelper::string()->msubstr($title,0, $length, 'utf-8', '..');
		}
		return $encode ? CHtml::encode($title) : $title;
	}
	
	/**
	 * 获取完整标题
	 * Enter description here ...
	 */
	public function getFullTitle() {
		$this->issetAttribute('title');
		return $this->owner->{$this->attributes['title']};
	}
	
	public function getVDesc(){
	    $this->issetAttribute('desc');
	    $str = $this->owner->{$this->attributes['desc']};
	    $str = str_replace('　', '', str_replace(' ', '', preg_replace("/<[^>]+>/","",preg_replace("/\s+/","", $str))));
	    return $str;
	}
	
	/**
	 * 列表地址
	 * Enter description here ...
	 */
	public function getMore() {
		$route = $this->route.'/'.$this->list;
		return Yii::app()->createUrl($route);
	}
	
	/**
	 * 详情地址
	 * Enter description here ...
	 * @param unknown_type $params
	 */
	public function getVurl($params = array()) {
		$route = $this->route.'/'.$this->view;
		return Yii::app()->createUrl($route, 
			empty($params) ? array('id'=>$this->owner->primaryKey) : $params
		);
	}
	
	public function issetAttribute($attribute) {
		if (!isset($this->attributes[$attribute]))
			throw new CHttpException(404,get_class($this->owner).'未设置"'.$attribute.'"对应的属性');
	}
}

?>